Page({
  data: {
    detailData: {
      title: '',
      text: '',
      imageList: [],
      imageUrl: '',  // 添加 imageUrl 字段
    },
  },

  onLoad(options) {
    const id = options.id;
    this.getAlbumDataById(id);
  },

  getAlbumDataById(id) {
    wx.request({
      url: `http://localhost:8080/tourisms/getTourismById?id=${id}`,
      method: 'GET',
      header: {
        'Content-Type': 'application/json',
      },
      success: (res) => {
        const data = res.data.data;
  
        // 更新页面数据
        this.setData({
          'detailData.title': data.title,
          'detailData.text': data.content,
          'detailData.imageList': [{ src: `http://localhost:8080/files/downloadFile?name=${data.imageUrl}` }],
          'detailData.imageUrl': `http://localhost:8080/files/downloadFile?name=${data.imageUrl}`,
        });
  
        // 发起保存数据的请求
        this.saveDataToServer(data);
      },
      fail: (error) => {
        console.error('Error fetching data from backend:', error);
      }
    });
  },
  
  // 发起保存数据的请求
  saveDataToServer(data) {
    wx.request({
      url: 'http://localhost:8080/history2s/addHistory2',
      method: 'POST',
      data: {
        // 将需要保存的数据传递给后端
        // 可根据实际情况修改下面的属性和属性值
        title: data.title,
        content: data.content,
        imageUrl: data.imageUrl,
        // 其他需要保存的属性...
      },
      header: {
        'Content-Type': 'application/json',
      },
      success: (res) => {
        console.log('Data saved successfully:', res.data);
      },
      fail: (error) => {
        console.error('Error saving data to backend:', error);
      }
    });
  },
  
});
